package cn.kbyue.txdemo.controller;

import cn.kbyue.txdemo.service.TxDemoService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/tx")
public class TxDemoController {

    @Resource
    private TxDemoService service;

    /**
     * 回滚简单示例
     */
    @GetMapping("rb/simple")
    public ResponseEntity<String> simple() {
        boolean updateName = service.txSimple();
        return ResponseEntity.ok("更新是否完成: " + updateName);
    }

    /**
     * 同类 this 调用, 事务失效
     */
    @GetMapping("rb/invalid")
    public ResponseEntity<String> txReference() {
        boolean updateName = service.txReference();
        return ResponseEntity.ok("更新是否完成: " + updateName);
    }

    /**
     * 同类 this 调用, 事务失效
     */
    @GetMapping("rb/valid")
    public ResponseEntity<String> txReferenceValid() {
        boolean updateName = service.txReferenceValid();
        return ResponseEntity.ok("更新是否完成: " + updateName);
    }

    /**
     * 方法调用的事务级别为 新建事务. 两个事务互相不影响
     */
    @GetMapping("rb/prop/new")
    public ResponseEntity<String> txPropagationNew() {
        boolean updateName = service.txPropagationNew();
        return ResponseEntity.ok("更新是否完成: " + updateName);
    }

    @GetMapping("find/insert/data")
    public ResponseEntity<String> findInsertData() {
        boolean insertData = service.findInsertData();
        return ResponseEntity.ok("是否查询到数据: " + insertData);
    }

}
